﻿using System;
using System.Collections;
using System.Collections.Generic;
//using IBatisNet.DataAccess;
using Wsitech.Model;
using Wsitech.Utility;

namespace Wsitech.BLL
{
    /// <summary>
    /// 功能：     AdminRole业务逻辑实现
    /// 作者：     LI
    /// 日期：     5/16/2012
    /// </summary>
    public class AdminRoleBLL : BaseBLL
    {
        private Wsitech.Persistence.Interface.IAdminRoleDao dao = null;

        private AdminRoleBLL()
        {
            dao = Wsitech.Persistence.Dao.AdminRoleSqlMapDao.Instance;
        }

        #region （懒汉）单例模式
        private static AdminRoleBLL instance = null;
        private static object syncRoot = new object();
        public static AdminRoleBLL Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new AdminRoleBLL();
                        }
                    }
                }
                return instance;
            }
        }
        #endregion


        #region	基本功能 不能修改


        /// <summary>
        /// 根据主键得到明细
        /// </summary>
        /// <returns></returns>
        public AdminRole GetDetail(System.String roleid)
        {
            try
            {
                return dao.GetDetail(roleid);
            }
            catch
            {
                throw;
            }
        }


        /// <summary>
        /// 得到列表
        /// </summary>
        /// <returns></returns>
        public IList<AdminRole> GetList()
        {
            try
            {
                IList<AdminRole> list = dao.GetList();
                return list;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        private IList<AdminRole> GetListByParams(object TopCounts, AdminRole adminrole)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("TopCounts", TopCounts);
                IList<AdminRole> list = dao.GetListByParams(ht);
                return list;
            }
            catch
            {
                throw;
            }
        }



        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="adminrole"></param>
        /// <returns></returns>
        public object Add(AdminRole adminrole)
        {
            try
            {
                LogInfoBLL.Instance.AddLog(UtilityEnum.Operation.Add, GetDetail(adminrole.RoleID));
                return dao.Add(adminrole);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="adminrole"></param>
        /// <returns></returns>
        public int Update(AdminRole adminrole)
        {
            try
            {
                LogInfoBLL.Instance.AddLog(UtilityEnum.Operation.Update, GetDetail(adminrole.RoleID));
                return dao.Update(adminrole);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="RoleID"></param>
        /// <returns></returns>
        public int Delete(System.String roleid)
        {
            try
            {
                return dao.Delete(roleid);
            }
            catch
            {
                throw;
            }
        }

        #endregion


        #region 扩展功能

        #endregion


        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="rid"></param>
        /// <param name="state"></param>
        public void ChangeState(string rid, Utility.UtilityEnum.State state)
        {
            AdminRole role = dao.GetDetail(rid);
            if (role != null)
            {
                role.State = (int)state;

                role.UpdateDate = DateTime.Now;

                Update(role);
            }
        }

        /// <summary>
        /// 添加新角色
        /// </summary>
        /// <param name="p"></param>
        /// <param name="p_2"></param>
        public void AddRole(string rolename, string description, string[] funList)
        {
            try
            {
                base.SqlMap.BeginTransaction();
                //添加角色
                AdminRole role = new AdminRole();
                role.RoleID = Guid.NewGuid().ToString();
                role.RoleName = rolename;
                role.Description = description;

                role.State = (int)UtilityEnum.State.Enable;
                role.IsDel = (int)UtilityEnum.Del.No;

                role.CreateDate = DateTime.Now;
                role.UpdateDate = DateTime.Now;

                Add(role);
                //添加权限
                foreach (string fun in funList)
                {
                    SiteMapInfo map = SiteMapInfoBLL.Instance.GetDetail(fun);
                    if (map != null)
                    {
                        RoleFuncRela rela = new RoleFuncRela();
                        rela.RelaId = Guid.NewGuid().ToString();
                        rela.RoleId = role.RoleID;
                        rela.FuncId = map.SitemapId;

                        RoleFuncRelaBLL.Instance.Add(rela);
                    }
                }

                base.SqlMap.CommitTransaction();

            }
            catch (Exception)
            {
                base.SqlMap.RollBackTransaction();
                throw;
            }

        }

        /// <summary>
        /// 更新角色信息
        /// </summary>
        /// <param name="rolename"></param>
        /// <param name="description"></param>
        /// <param name="funList"></param>
        public void UpdateRole(string roleid, string rolename, string description, string[] funList)
        {
            try
            {
                base.SqlMap.BeginTransaction();

                //修改角色数据
                AdminRole role = dao.GetDetail(roleid);
                if (roleid != null)
                {
                    role.RoleName = rolename;
                    role.Description = description;

                    role.UpdateDate = DateTime.Now;
                    Update(role);
                }
                //删除旧权限
                RoleFuncRelaBLL.Instance.DeleteByRoleId(roleid);
                //添加新权限
                foreach (string fun in funList)
                {
                    SiteMapInfo map = SiteMapInfoBLL.Instance.GetDetail(fun);
                    if (map != null)
                    {
                        RoleFuncRela rela = new RoleFuncRela();
                        rela.RelaId = Guid.NewGuid().ToString();
                        rela.RoleId = role.RoleID;
                        rela.FuncId = map.SitemapId;

                        RoleFuncRelaBLL.Instance.Add(rela);
                    }
                }

                base.SqlMap.CommitTransaction();
            }
            catch (Exception)
            {
                base.SqlMap.RollBackTransaction();
                throw;
            }
        }



        /// <summary>
        /// 获取所有未禁用的角色
        /// </summary>
        /// <returns></returns>
        public IList<AdminRole> GetActiveList()
        {
            Hashtable ht = new Hashtable();
            ht.Add("state", (int)UtilityEnum.State.Enable);
            ht.Add("isDel", (int)UtilityEnum.Del.No);
            return dao.GetListByParams(ht);
        }


        /// <summary>
        /// 根据用户ID获取角色列表
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public IList<AdminRole> GetRoleListByUserId(string userid)
        {
            return dao.GetRoleListByUserId(userid);
        }
    }
}
